<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-7-11
 * Time: 11:01
 */
namespace wxpay;

class Message
{
    /**
     * 发送模板消息
     * @param $openId
     * @param $describe
     */
    public function sendMessage($openId,$template,$data)
    {
        $token = (new GetToken())::request();
        $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$token;
        $param = [
          'type' => 'post',
          'url' =>  $url
        ];

        $data = [
            'touser' =>$openId,
            'template_id' =>$template,//'fdINUMkfiCO99tXOhESiPgnsXXRzr3gwaP3nyoZmaf8',
            'data' =>$data
         ];
        $data = json_encode($data);

       return  $this->curl($param,$data);

    }

    /**
     * 发送请求
     * @param $param
     * @param null $data
     * @return string
     */
    public function curl($param,$data=null)
    {
        $ch = curl_init();

        curl_setopt($ch,CURLOPT_URL,$param['url']);
        if (isset($param['type'])) {
            curl_setopt($ch,CURLOPT_POST,1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
        }

         $res = json_encode(curl_exec($ch));
         return $res;
    }

}